const moment = require('moment')
const bcrypt = require('bcrypt')
// exports.relativeTime = time=>moment(new Date(time)).format('YYYY-MM-DD HH:mm:ss');
module.exports = {
    moment,
    genSaltPassword(password) {
        return new Promise((resolve, reject) => {
            bcrypt.genSalt(10, (err, salt) => {
                bcrypt.hash(password, salt, (err, hash) => {
                    if (!err) {
                        resolve(hash)
                    } else {
                        reject(err)
                    }
                })
            })
        })

    },
    comparePassword(_password, saltPassword) {
        return new Promise((resolve, reject) => {
            bcrypt.compare(_password, saltPassword, (err, isMatch) => {
                if (!err) resolve(isMatch)
                else reject(err)
            })
        })
    },
    success( {ctx, res = null} ) {
        ctx.status = res.status ? res.status : 200
        if (res.status) {
            delete res.status
        }
        ctx.body = {
            ...res,
            data: res.data ? res.data : null,
            code: res.code ? res.code : 200,
            msg: res.msg ? res.msg : '请求成功'
        }
    }
}
